<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Advertising Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage News  
 * @author Manh Nguyen - 03/08/2012 
 * @link http://sunsoft.vn
 */
class Advertising_model extends DataMapper {

    var $model = 'advertising_model';
    var $table = 'advertising';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
        $this->active = 1;
        $this->validation   =  array(
            'position' => array(
                'values' => array(
                    'middle'        => $this->lang->line('middle'),
                    'top'           => $this->lang->line('top'),
                    'left'          => $this->lang->line('left'),
                    'ads_middle'  => $this->lang->line('ads_middle'),
                    'top_left'      => $this->lang->line('top_left'),
                    'right'         => $this->lang->line('right'),
                    'top_right'     => $this->lang->line('top_right'),
                    'bottom_right'  => $this->lang->line('bottom_right'),
        			'bottom'  		=> $this->lang->line('bottom'),
        			'ads_right'  		=> $this->lang->line('ads_right'),
                )
            ),
            
            'active' => array(
                'values' => array(
                    '0' => $this->lang->line('no'),
                    '1' => $this->lang->line('yes')
                )
            ),
            
            'partner_id' => array(
             ),
             
            'type'=> array(
                'values' => array(
                    'image'     => $this->lang->line('image'),
                    'youtube'   => $this->lang->line('youtube'),
                    'flash'     => $this->lang->line('flash'),
                    'code'      => $this->lang->line('code'),
                )
            )
        ); 
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------	
    var $validation = array();

    /**
     * Default order in query select 
     */
    var $default_order_by = array('order');

    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? $this->localize_label('unset') : $this->title;
    }

    /**
     * string value of active
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_position() {
        return $this->validation['position']['values'][$this->position];
    }

    /**
     * string value of active
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_active() {
        return $this->validation['active']['values'][$this->active];
    }
    
    function get_resource() {
    	switch($this->type) {
    		case 'code':
			case 'popup':
    			return '<xmp>' . $this->resource . '</xmp>';
    			break;
			case 'image':
				return '<img src="' . base_url() . $this->resource . '" alt="' . $this->title . '" />';
				break;
    		default:
    			return $this->resource;
    			break;
    	}
    }

    /**
     * array position of object
     *
     * @access public
     * @param bool
     * @return array
     */
    function position_list($zero = FALSE) {
        $position_list = $this->validation['position']['values'];
        if ($zero) {
            $position_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $position_list);
        }
        return $position_list;
    }
    
    /**
     * array type of object
     *
     * @access public
     * @param bool
     * @return array
     */
    function type_list($zero = FALSE) {
        $type_list = $this->validation['type']['values'];
        if ($zero) {
            $type_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $type_list);
        }
        return $type_list;
    }
    
    public function get_by_position($position = NULL) {
    	$this->db->select('id, title, link, type, resource');
        $this->db->from('advertising');
    	$this->db->where('active', 1);
    	$this->db->where('position', $position);
    	return $this->db->get()->result();
    }

    /**
     * array active of object
     *
     * @access public
     * @param bool
     * @return array
     */
    function active_list($zero = FALSE) {
        $active_list = $this->validation['active']['values'];
        if ($zero) {
            $active_list = array_merge(array('' => '--'.$this->lang->line('all').'--'), $active_list);
        }
        return $active_list;
    }
    
    function statistics_by_position(){
    	$this->select("count(id) as count, position, active");
    	$this->group_by("position, active");
    	$this->get();
    	$array = array();
    	foreach($this as $item){
    		$array[$item->position][$item->active] = $item->count;
    	}
    	return $array;
//     	print_r($array);
//     	exit;
    }

    function get_advertising($position, $limit, $partner_code) {
        if (!empty($position)) {
            $this->where('position', $position);
            $this->where('active', 1);
            $this->order_by('created', 'desc');
            if ($limit) {
                $this->limit($limit);
            }
            return $this->get();
        } else {
            return 0;
        }
    }
    
    /**
     * array metadata for generate jquery grid. 
     * 
     * @access public
     * @param null
     * @return array
     */
    function jqgrid_metadata() {
        //edittype: 'text', 'textarea', 'select', 'checkbox', 'password', 'button', 'image', 'file', 'custom' 
        return array(
            'names' => array(
                'id',
                $this->lang->line('title'),
                $this->lang->line('url'),
                $this->lang->line('resource'),
                $this->lang->line('position'),
                $this->lang->line('order'),
                $this->lang->line('active')
            ),
            'model' => array(
                array('name' => 'id', 'hidden' => TRUE, 'key' => TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'url',
                    'search' => FALSE
                ),
                array('name' => 'image',
                    'search' => FALSE
                ),
                array('name' => 'position',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->position_list(TRUE)
                    ),
                    'stype' => 'select'
                ),
                array('name' => 'order',
                    'searchoptions' => array(
                        'sopt' => array('eq', 'gt', 'ge', 'lt', 'le')
                    )
                ),
                array('name' => 'active',
                    'searchoptions' => array(
                        'sopt' => array('eq'),
                        'value' => $this->active_list(TRUE)
                    ),
                    'stype' => 'select'
                )
            )
        );
    }

}

/* End of file advertising_model.php */
/* Location: ./application/models/advertising_model.php */